/*
 *
 *
 *
 */
package com.cms.controller.admin;

import com.cms.domain.Config;
import com.cms.domain.Feedback;
import com.cms.enums.HtmlType;
import com.cms.service.CategoryService;
import com.cms.service.HtmlService;
import com.cms.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import java.util.*;

/**
 * Controller - 静态化
 *
 *
 *
 */
@Controller("adminHtmlController")
@RequestMapping("/admin/html")
public class HtmlController extends BaseController {

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private HtmlService htmlService;

	/**
	 * 生成静态
	 */
	@RequestMapping
	public String index(ModelMap modelMap){
		modelMap.addAttribute("categoryTree", categoryService.findTree(null,getCurrentSite().getId()));
		return getView("html/index");
	}

	/**
	 * 生成静态
	 */
	@ResponseBody
	@RequestMapping("/generate")
	public Feedback generate(Integer type,Integer categoryId) {
		Config config = SystemUtils.getConfig();
		if(config.getSiteModel()!= Config.SiteModel.HTML.ordinal()){
			return Feedback.error("网站模式不是静态模式");
		}
		int generateCount = 0;
		if(type== HtmlType.INDEX.ordinal()){
			generateCount = htmlService.generateIndex(getCurrentSite().getId());
		}else if(type==HtmlType.CONTENT.ordinal()){
			generateCount += htmlService.generateContent(categoryId,getCurrentSite().getId());
		}else if(type==HtmlType.CATEGORY.ordinal()){
			generateCount += htmlService.generateCategory(categoryId,getCurrentSite().getId());
		}else{
			generateCount += htmlService.generateAll(getCurrentSite().getId());
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("generateCount", generateCount);
		return Feedback.success(data);
	}

}